<?php namespace Home\Controller; 

use Hdphp\Controller\Controller;

//前台内容页控制器
class ContentController extends Controller{
	private $model;
	public function __init(){
		//实例化商品表模型
		$this->model = new \Common\Model\shopGoods;
	}
	
	//默认
	public function index(){
		//1.显示左侧商品图册和商品表(商品名称,价钱)
		//接收gid
		$gid = Q('get.gid',0,'intval');
		$goodsDetailData = $this->model->join('shop_goods_detail','gid','=','detail_gid')->where("detail_gid={$gid}")->find();
		//把商品详细表里面gd_pic(字符串形式转成数组形式)
		$goodsDetailData['gd_pic'] = explode(',', $goodsDetailData['gd_pic']);
		View::with('goodsDetailData',$goodsDetailData);
//		p($goodsDetailData);
		//2.显示右侧(商品属性表和类型属性表关联)
		$goodsAttrModel = new \Common\Model\shopGoodsAttr;
		$goodsAttrData = $goodsAttrModel->join('shop_type_arrt','type_taid','=','taid')->where("goods_gid={$gid} AND tatype=1")->get();
		$data = array();
		foreach ($goodsAttrData as $k => $v) {
			$data[$v['taname']][] = $v;
		}
		View::with('data',$data);
//		p($data);
		//3.规格参数(商品表关联类型属性表)
		$typeData = $this->model->join('shop_type_arrt ta','ta.tid','=','shop_goods.tid')->where("gid={$gid} AND tatype=0")->get();
		View::with('typeData',$typeData);
//		p($typeData);
		//默认模板
	    View::make();
	}
	
	//颜色、尺码异步提交
	public function site(){
		//1.接收传递过来的str(库存,字符串形式的,因为多个,所以删除掉最后一个)
		$str = rtrim(Q('post.str'),',');
		//实例化货品列表
		$goodsListModel = new \Common\Model\shopGoodsList;
		$goodsListData = $goodsListModel->where("combine='{$str}'")->pluck('number');
		//2.接收传递过来的extraprice(附加价格,字符串形式的,因为多个,所以删除掉最后一个)
		$extraprice = rtrim(Q('post.extraprice'),',');
		//转成数组
		$arr = explode(',', $extraprice);
		//把价格相加
		$num = 0;
		foreach ($arr as $v) {
			$num = $num + $v;
		}
		$data = array(
			'combine' => $goodsListData,
			'extraprice' => $num
		);
		echo json_encode($data);exit;
	}
	
	//购物车异步提交
	public function cart(){
		//把异步发送来的属性存入
		$data = Q('post.');
		//调用框架购物车方法,自动存入$_SESSION
		Cart::add($data);
		$num = Cart::getTotalNums($data);
		echo json_encode($num);exit;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}